package start.demo01.fileIO;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileLink {

    @Test
    public void t0() throws Exception {
//        String p1 = "E:\\SteamLibrary\\steamapps\\workshop\\content\\431960";//wall-paper
        String p1 = "E:\\filePath\\video";
//        String p2 = "C:\\Users\\wangcj\\Desktop\\video\\";//桌面目标目录
        String p2 = "C:\\Install\\nginx\\nginx-1.20.2\\video\\rion\\";//nginx目录
        FileManage.cleanFile(p2);
        link(p1,p2);
    }
    public static void link(String p1,String p2){
        File f1 = new File(p1);
        File[] dirs = f1.listFiles();
        for (File dir : dirs) {
            if (dir.isDirectory()) {
                File[] files = dir.listFiles((f, n) -> n.endsWith(".mp4")||n.endsWith(".avi"));
                if (files.length > 0) {
                    Path target = files[0].toPath();
                    Path link = Paths.get(p2 + files[0].getName());
                    try {
                        Files.createSymbolicLink(link, target);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                String name = dir.getName();
                if(name.endsWith(".mp4")||name.endsWith("avi")){
                    Path target = dir.toPath();
                    Path link = Paths.get(p2 + dir.getName());
                    try {
                        Files.createSymbolicLink(link, target);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
